package de.szut.fragments.dialogs;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.Button;
import de.szut.logic.Logic;
import de.szut.scene.scenes.GameScene;
import de.szut.splasher.R;

/**
 * Created with IntelliJ IDEA.
 * User: nbothmer
 * Date: 25.10.13
 * Time: 09:23
 * To change this template use File | Settings | File Templates.
 */
public class GamePausedDialog extends DialogFragment implements OnClickListener {

    private GameScene gameScene;
    private Dialog mDialog;
    private Button resume, exit, saveAndExit;
    private Logic logic;

    public GamePausedDialog() {
    }

    public GamePausedDialog(GameScene gameScene, Logic logic) {
        this.gameScene = gameScene;
        this.logic = logic;
    }

    public static GamePausedDialog newInstance(GameScene gameScene, Logic logic) {
        GamePausedDialog dlg = new GamePausedDialog(gameScene, logic);
        return dlg;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.game_paused_dialog, container, false);
        mDialog = getDialog();
        mDialog.setCancelable(false);
        mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        resume = (Button) root.findViewById(R.id.resume_btn);
        resume.setOnClickListener(this);
        exit = (Button) root.findViewById(R.id.exit_btn);
        exit.setOnClickListener(this);
        saveAndExit = (Button) root.findViewById(R.id.save_exit_btn);
        saveAndExit.setOnClickListener(this);
        saveAndExit.setEnabled(false);
        if (logic.gameHasStarted()) {
            saveAndExit.setEnabled(true);
        }
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == event.KEYCODE_BACK) {
                    return true;
                }
                return false;  //To change body of implemented methods use File | Settings | File Templates.
            }
        });
        return root;  //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.resume_btn:
                this.gameScene.resumeToGame();
                break;
            case R.id.exit_btn:
                this.gameScene.exitGame();
                break;
            case R.id.save_exit_btn:
                this.gameScene.saveAndExitGame();
                break;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onDestroy() {
        super.onDestroy();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (!this.gameScene.isTimeIsRunning() && this.logic.gameHasStarted()) {
            this.gameScene.reRunTimer();
        }//To change body of overridden methods use File | Settings | File Templates.
    }

}
